﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Moon.Orm;


namespace IDAL
{
    /// <summary>
    /// 基本服务 提供增、删、改、按ID查 等通用功能
    /// </summary>
    /// <typeparam name="T">实体类</typeparam>
    public class BaseDao<T> : IBaseDao<T> where T : new ()
    {



        public object Save(EntityBase entity)
        {
            return DBFactory.Add(entity);
        }

        public void Update(EntityBase entity)
        {
           DBFactory.Update(entity);
            

        }

        /// <summary>
        /// 删除
        /// </summary>
        public long Delete(Field id)
        {

            return DBFactory.DeleteWhen(id);
        }

        public long Delete(string sqlwhere)
        {
            return DBFactory.DeleteWhen(sqlwhere, DBFactory.DefaultDB);
        }

       
        /// <summary>
        /// 按条件查询
        /// </summary>
        public T GetByID(Field id)
        {

            return DBFactory.GetEntity<T>(id);
        }

        public long GetCount(Field sqlwhere)
        {
            return DBFactory.GetCount(sqlwhere);
        }

        public long GetCount(string sqlwhere)
        {
            return DBFactory.GetCount(sqlwhere);
        }

        public List<T> GetEntities(Field sqlwhere)
        {
            return DBFactory.GetEntities<T>(sqlwhere);
        }
        public List<T> GetEntities(string sqlwhere)
        {
            return DBFactory.GetEntities<T>(sqlwhere);
        }

        public bool Exists(Field sqlwhere)
        {
            return DBFactory.Exists(sqlwhere);
        }

        public bool Exists(string sqlwhere)
        {
            
            return DBFactory.Exists(sqlwhere);
        }

        public List<T> GetWebPagedListDesc(Field zhujian, Field sqlwhere)
        {
            return DBFactory.DefaultDB.GetWebPagedListDesc<T>(zhujian, sqlwhere);
        }

        public List<T> GetWebPagedListDesc(Field zhujian, Field sqlwhere, long sumcount)
        {
            return DBFactory.DefaultDB.GetWebPagedListDesc<T>(zhujian, sqlwhere, sumcount);
        }

        public int ExecuteOneSql(string sqlwhere)
        {
            return DBFactory.DefaultDB.ExecuteOneSql(sqlwhere);
        }

        public DataSet ExecuteProToDataSet(List<DbParameter> Parameter, string porname)
        {
            return DBFactory.DefaultDB.ExecuteProToDataSet(Parameter,porname);

        }
    }
}
